﻿using Newtonsoft.Json;
using OdooRpcClient.Model.Hr;
using PortaCapena.OdooJsonRpcClient;
using PortaCapena.OdooJsonRpcClient.Consts;
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Runtime.Serialization;
using System.Threading.Tasks;
using WebAssistant.BusinessLogic;
using WebAssistant.Common;

namespace Assistant.Report.Method.Hr
{
    /// <summary>
    /// 打印休假摘要方法
    /// </summary>
    public class ReportHolidaysSummary: ReportAction
    {
        private static DateTime CurrentMonth;

        public static async Task<DataTable> GetHolidaysSummaryContextDataTable()
        {
            DataTable dt = new DataTable("Employees");

            //获取部门名称
            var departmentRepository = new OdooRepository<HrDepartmentOdooModel>(Shared.odooConfig);
            departmentRepository.Config.Context.Language = context.lang;
            var departmens = await departmentRepository.Query()
                .Select(x => new
                {
                    x.Id,
                    x.Name,
                })
                .ToListAsync();

            //获取职位名称
            var jopRepository = new OdooRepository<HrJobOdooModel>(Shared.odooConfig);
            jopRepository.Config.Context.Language = context.lang;
            var jops = await jopRepository.Query()
                .Select(x => new
                {
                    x.Id,
                    x.Name,
                })
                .ToListAsync();

            if (departmens.Succeed && jops.Succeed)
            {
                //获取员工
                var employeeRepository = new OdooRepository<HrEmployeeOdooModel>(Shared.odooConfig);
                employeeRepository.Config.Context.Language = context.lang;
                var employees = await employeeRepository.Query()
                    .Where(x => x.Id, OdooOperator.In, context.active_ids)
                    .Select(x => new
                    {
                        x.Barcode,
                        x.CompanyId,
                        x.DepartmentId,
                        x.DisplayName,
                        x.Image512,
                        x.Name,
                        x.JobId,
                    })
                    .ToListAsync();
                dt.Columns.Add("Barcode", typeof(string));
                dt.Columns.Add("BarcodeImage", typeof(string));
                dt.Columns.Add("CompanyId", typeof(int));
                dt.Columns.Add("CompanyLogo", typeof(string));
                dt.Columns.Add("DepartmentId", typeof(int));
                dt.Columns.Add("DepartmentName", typeof(string));
                dt.Columns.Add("DisplayName", typeof(string));
                dt.Columns.Add("Image512", typeof(string));
                dt.Columns.Add("JobId", typeof(int));
                dt.Columns.Add("JobTitleName", typeof(string));
                dt.Columns.Add("Name", typeof(string));

                foreach (var employee in employees.Value)
                {
                    string barcodeImage = ReportHelper.GenerateBarcodeString(employee.Barcode, 20, 50);
                    employee.GetType().GetProperty("BarcodeImage").SetValue(employee, barcodeImage, null);

                    string departmentName = (from departmen in departmens.Value
                                             where departmen.Id == employee.DepartmentId
                                             select new { Name = departmen.Name }
                                           ).First().Name;
                    employee.GetType().GetProperty("DepartmentName").SetValue(employee, departmentName, null);

                    string companyLogo = DataAccess.GetCompanyLogoBase64String(Convert.ToInt32(employee.CompanyId));
                    employee.GetType().GetProperty("CompanyLogo").SetValue(employee, companyLogo, null);

                    string jobTitleName = (from jop in jops.Value
                                           where jop.Id == employee.JobId
                                           select new { JobTitleName = jop.Name }
                                           ).First().JobTitleName;
                    employee.GetType().GetProperty("JobTitleName").SetValue(employee, jobTitleName, null);
                    //badgeEmployees.Add(employee);
                    dt.Rows.Add(
                        employee.Barcode,
                        employee.BarcodeImage,
                        employee.CompanyId,
                        employee.CompanyLogo,
                        employee.DepartmentId,
                        employee.DepartmentName,
                        employee.DisplayName,
                        employee.Image512,
                        employee.JobId,
                        employee.JobTitleName,
                        employee.Name
                        );
                }
            }


            return dt;
        }

        public static DataTable GetHolidaysSummaryDataDataTable()
        {
            DataTable dt = new DataTable("Parameters");

            dt.Columns.Add("Title", typeof(string));
            //dt.Columns.Add("CreateUid", typeof(int));
            //dt.Columns.Add("CreateName", typeof(string));
            dt.Columns.Add("StartDate", typeof(string));
            dt.Columns.Add("EndDate", typeof(DateTime));
            dt.Columns.Add("HolidayType", typeof(string));

            DataRow dr = dt.NewRow();
            dr["Title"] = action.Name;

            Dictionary<string, object> dict = data;

            foreach (var item in dict)
            {
                if (item.Key == "form")
                {
                    dr["StartDate"] = item.Value;
                    var form = JsonConvert.DeserializeObject<Dictionary<object, object>>(item.Value.ToString());
                    foreach (var subItem in form)
                    {
                        if (subItem.Key.ToString() == "date_from")
                        {
                            string dateString = subItem.Value.ToString();
                            DateTime StartDate = DateTime.ParseExact(dateString, "yyyy-MM-dd", null);

                            CurrentMonth = StartDate;

                            dr["StartDate"] = StartDate;
                            //dr["EndDate"] = StartDate.AddDays(59);
                            dr["EndDate"] = DateTimeHelper.LastDayOfNextMonth(StartDate);
                        }
                        if(subItem.Key.ToString() == "holiday_type")
                        {
                            dr["HolidayType"] = subItem.Value.ToString();
                        }
                    }

                }
            }

            dt.Rows.Add(dr);
            return dt;
        }

        public static DataTable GetMonthsAndDays()
        {
            DataTable dt = new DataTable("Months");
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Days", typeof(int));

            DataRow drCurrentMonth = dt.NewRow();
            drCurrentMonth["Name"] = CurrentMonth.Month.ToString();
            drCurrentMonth["Days"] = System.Threading.Thread.CurrentThread.CurrentUICulture.Calendar.GetDaysInMonth(CurrentMonth.Year, CurrentMonth.Month);
            dt.Rows.Add(drCurrentMonth);

            DateTime NextMonth = DateTimeHelper.LastDayOfNextMonth(CurrentMonth);
            DataRow drNextMonth = dt.NewRow();
            drNextMonth["Name"] = NextMonth.Month.ToString();
            drNextMonth["Days"] = System.Threading.Thread.CurrentThread.CurrentUICulture.Calendar.GetDaysInMonth(NextMonth.Year, NextMonth.Month);

            return dt;
        }
    }
}
